package com.superman.view;

import com.superman.entity.Record_User_Car;
import com.superman.entity.User;
import com.superman.util.JPanelRecord;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class RentUI {
    private User user;
    Query query = new Query();
    RentAndReturn rentAndReturn = new RentAndReturn();
    JPanelRecord jPanelRecord = new JPanelRecord();

    private JFrame frame = new JFrame("用户租车业务办理");
    private Container container = frame.getContentPane();

    //上侧
    private JPanel upside_panel = new JPanel();
    private JLabel car_lab = new JLabel("请选择您想租的汽车：");
    private JComboBox<String> car_cb = new JComboBox<>(query.rentQueryCar());
    private JButton rent_btn = new JButton("确认租车");
    private JButton backup_btn = new JButton("返回");
    private JButton quit_btn = new JButton("退出");

    //中部
    private JPanel middle_panel = new JPanel();
    private JPanel rent_panel;

    //下侧
    private JPanel downside_panel = new JPanel();
    private JLabel result_lab = new JLabel("欢迎使用二嗨租车");

    public RentUI(User user){
        frame.setSize(1000,500);
        container.setLayout(new BorderLayout());
        initFrame(user);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private void initFrame(User user){
        //上侧
        upside_panel.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));
        upside_panel.add(car_lab);
        upside_panel.add(car_cb);
        upside_panel.add(rent_btn);
        upside_panel.add(backup_btn);
        upside_panel.add(quit_btn);
        container.add(upside_panel, "North");

        //中部
        CardLayout cardLayout = new CardLayout(20, 20);//设置布局管理器为卡片式
        middle_panel.setLayout(cardLayout);
        container.add(middle_panel, "Center");

        //下侧
        downside_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 30));
        downside_panel.add(result_lab);
        container.add(downside_panel, "South");

        rent_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String choose = String.valueOf(car_cb.getSelectedItem());
                String car_number = choose.split("-")[0];
                List<Record_User_Car> list = rentAndReturn.rentCar(user.getId(), car_number);
                if (list.isEmpty()){
                    result_lab.setText("租车失败！");
                }else {
                    result_lab.setText("租车成功！");
                    rent_panel = jPanelRecord.getJPanelRecord(list);
                    middle_panel.add("租车成功信息", rent_panel);
                }
            }
        });

        backup_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                hide();
                new UserUI(user);
            }
        });

        quit_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                hide();
                new LoginUI();
            }
        });
    }

    public void hide(){
        frame.setVisible(false);
    }

}
